// PBM_Info_1.c ( 112 )
// The programme reads the header and determines the width or the height of the original image .
// --Width ( This causes the programme to output the width of the image . )
// --Height ( This causes the programme to output the height of the image . )
// --Human ( This causes the programme to output a labelled listing of both values . )
// --Help ( This prints the instructions and continues processing . )
// --help ( This prints the instructions and cancels image processing . )
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
char* NumberFilter2( char* Input1 , int Cap1 , int Cap2 ) ;
int main( int argc , char** argv ) {
  int CharBuff1 ;
  char CharBuff2 ;
  int State1 ;
  int Error1 ;
  int Count1 ;
  int Count2 ;
  int Width1 ;
  int Height1 ;
  Count1 = 1 ;
  Count2 = 0 ;
  Error1 = 0 ;
  State1 = 0 ;
  char* ValueBuffer1 = malloc( 9 * sizeof( char ) ) ;
  if ( ValueBuffer1 == NULL ) {
    fprintf( stderr , "Either there are memory problems or something has gone horribly wrong .\n" ) ;
    return 1 ;
  }
  while ( Count1 < argc  ) {
    if ( strcmp( argv[ Count1 ] , "--Width" ) == 0 ) {
      Count2 = 1 ;
    } else if ( strcmp( argv[ Count1 ] , "--Height" ) == 0 ) {
      Count2 = 2 ;
    } else if ( strcmp( argv[ Count1 ] , "--Human" ) == 0 ) {
      Count2 = 4 ;
    } else if ( strcmp( argv[ Count1 ] , "--Help" ) == 0 ) {
      fprintf( stderr , "PBM_Info_1.c ( 112 )\n" ) ;
      fprintf( stderr , "This programme prints the width or the height of a pbm file .\n" ) ;
      fprintf( stderr , "  --Width ( This causes the programme to output the width of the image . )\n" ) ;
      fprintf( stderr , "  --Height ( This causes the programme to output the height of the image . )\n" ) ;
      fprintf( stderr , "  --Human ( This causes the programme to output a labelled listing of both values . )\n" ) ;
      fprintf( stderr , "  --Help ( This prints the instructions and continues processing . )\n" ) ;
      fprintf( stderr , "  --help ( This prints the instructions and cancels image processing . )\n" ) ;
    } else if ( strcmp( argv[ Count1 ] , "--help" ) == 0 ) {
      State1 = 0 - 1 ;
      fprintf( stderr , "PBM_Info_1.c ( 112 )\n" ) ;
      fprintf( stderr , "This programme prints the width or height of a pbm file .\n" ) ;
      fprintf( stderr , "  --Width ( This causes the programme to output the width of the image . )\n" ) ;
      fprintf( stderr , "  --Height ( This causes the programme to output the height of the image . )\n" ) ;
      fprintf( stderr , "  --Human ( This causes the programme to output a labelled listing of both values . )\n" ) ;
      fprintf( stderr , "  --Help ( This prints the instructions and continues processing . )\n" ) ;
      fprintf( stderr , "  --help ( This prints the instructions and cancels image processing . )\n" ) ;
    }
    Count1 ++ ;
  }
  Count1 = 0 ;
  if ( State1 >= 0 ) {
    CharBuff1 = getchar( ) ;
  } else {
    CharBuff1 = -1 ;
  }
  // We check that the magic number is good .
  if ( CharBuff1 == ( int )'P' ) {
    CharBuff1 = getchar( ) ;
    if ( CharBuff1 == ( int )'4' ) {
      State1 = 1 ;
      CharBuff1 = getchar( ) ;
    } else {
      Error1 = 19 ; // This means that the magic number is wrong .
    }
  } else {
    Error1 = 18 ; // This means that the magic number prefix is wrong . This is generally a bad sign .
  }
  if ( ( CharBuff1 >= 10 ) && ( State1 == 1 ) && ( CharBuff1 <= 32 ) ) {
    State1 = 2 ;
  } else {
    Error1 = 20 ; // This means that the magic number is not followed by white space .
  }
  // We chew through white space .
  while ( ( CharBuff1 >= 10 ) && ( State1 == 2 ) && ( CharBuff1 <= 32 ) ) {
    CharBuff1 = getchar( ) ;
  }
  // Check that we have a number of some sort .
  if ( ( CharBuff1 >= 48 ) && ( CharBuff1 <= 58 ) && ( State1 == 2 ) ) {
    State1 = 3 ;
  } else {
    Error1 = 22 ; // This means that the white space is followed by something other than a number . Because we measure the height in decimal digits , letters and other non-numeral characters tend to cause problems .
  }
  // We eliminate leading zeroes so that we have no accidental interpretation as octal . These leading zeroes may be cause for concern , but not necessarily for a refusal of the programme to operate .
  while ( ( CharBuff1 == '0' ) && ( State1 == 3 ) ) {
    CharBuff1 = getchar( ) ;
  }
  if ( ( CharBuff1 >= 48 ) && ( CharBuff1 <= 58 ) && ( State1 == 3 ) ) {
    State1 = 4 ;
  } else {
    Error1 = 23 ; // This means that the zeroes are followed by something other than a number . This means that we either have non-decimal digits or a width of zero pixels . Both promise to pose problems .
  }
  // We chew through the first number , which ought to give the image width by pixel .
  Count1 = 0 ;
  while ( ( CharBuff1 >= 48 ) && ( State1 == 4 ) && ( CharBuff1 <= 58 ) && ( Count1 < 7 ) ) {
    ValueBuffer1[ Count1 ] = ( char )CharBuff1 ;
    Count1 ++ ;
    CharBuff1 = getchar( ) ;
  }
  if ( ( Count1 > 0 ) && ( State1 == 4 ) ) {
    ValueBuffer1[ Count1 ] = ( char )0 ; // We terminate the string .
    sscanf( ValueBuffer1 , "%d" , &Width1 ) ;
    if ( Width1 < 32767 ) {
      State1 = 5 ;
    } else {
      Error1 = 25 ; // This means that the horizontal dimension is too great . For performance reasons , we want to be able to reference bits by an integer .
    }
  } else {
    Error1 = 24 ; // This means that we have retrieved no digits . As the programme is currently arranged , this error is impossible . The prerequisite of proceeding to step four is that the programme has already retrieved a non-zero decimal digit . This check may be necessary later .
  }
  // We chew through white space .
  while ( ( CharBuff1 >= 10 ) && ( State1 == 5 ) && ( CharBuff1 <= 32 ) ) {
    CharBuff1 = getchar( ) ;
  }
  // Check that we have a number of some sort .
  if ( ( CharBuff1 >= 48 ) && ( CharBuff1 <= 58 ) && ( State1 == 5 ) ) {
    State1 = 6 ;
  } else {
    Error1 = 26 ; // This means that the white space is followed by something other than a number . Because we measure the height in decimal digits , letters and other non-numeral characters tend to cause problems .
  }
  // We eliminate leading zeroes so that we have no accidental interpretation as octal . These leading zeroes may be cause for concern , but not necessarily for a refusal of the programme to operate .
  while ( ( CharBuff1 == '0' ) && ( State1 == 6 ) ) {
    CharBuff1 = getchar( ) ;
  }
  if ( ( CharBuff1 >= 48 ) && ( CharBuff1 <= 58 ) && ( State1 == 6 ) ) {
    State1 = 7 ;
  } else {
    Error1 = 27 ; // This means that the zeroes are followed by something other than a number . This means that we either have non-decimal digits or a width of zero pixels . Both promise to pose problems .
  }
  // We chew through the second number , which ought to give the image height by pixel .
  Count1 = 0 ;
  while ( ( CharBuff1 >= 48 ) && ( State1 == 7 ) && ( CharBuff1 <= 58 ) && ( Count1 < 7 ) ) {
    ValueBuffer1[ Count1 ] = CharBuff1 ;
    Count1 ++ ;
    CharBuff1 = getchar( ) ;
  }
  if ( ( Count1 > 0 ) && ( State1 == 7 ) ) {
    ValueBuffer1[ Count1 ] = ( char )0 ; // We terminate the string .
    sscanf( ValueBuffer1 , "%d" , &Height1 ) ;
    if ( Height1 < 32767 ) {
      State1 = 8 ;
    } else {
      Error1 = 29 ; // This means that the vertical dimension is too great . For performance reasons , we want to be able to reference bits by an integer .
    }
  } else {
    Error1 = 28 ; // This means that we have retrieved no digits . As the programme is currently arranged , this error is impossible . The prerequisite of proceeding to step four is that the programme has already retrieved a non-zero decimal digit . This check may be necessary later .
  }
  // We clear any data remaining in the pipe .
  while ( CharBuff1 >= 0 ) {
    CharBuff1 = getchar( ) ;
  }
  if ( ( Count2 == 1 ) && ( State1 == 8 ) ) {
    printf( "%d" , Width1 ) ;
  }
  if ( ( Count2 == 2 ) && ( State1 == 8 ) ) {
    printf( "%d" , Height1 ) ;
  }
  free( ValueBuffer1 ) ;
  return 0 ;
}

